//
//  FJReportView.swift
//  FruitJuice
//
//  Created by Rui on 2023/12/17.
//

import UIKit
import SwiftEntryKit

class FJReportView: UIView, SAAnimationDelegate, SAAnimationProtocol {
    
    func animationContainerViewSize() -> CGSize {
        CGSize(width: .screenWidth, height: .screenHeight)
    }
    
    func animationContainerViewStyle() -> SAAnimationViewStyle {
        .center
    }
    
    func animationContainerScreenInteraction() -> EKAttributes.UserInteraction {
        .dismiss
    }
    
//    func animation(customAttributes attributes: EKAttributes) -> EKAttributes {
//        var attri = attributes
//        attri.positionConstraints.verticalOffset = .bottomSpace
//        return attri
//    }
    
    private lazy var backView           = c_make_ex(UIView.self)
        .bgColor(.white).corner(28.5).end()
    private lazy var titleLabel         = c_make_ex(UILabel.self)
        .alignment(.center).font(21.font_bold)
        .color(.black).text("Report").end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .clear
        tableView.tableFooterView = UIView(frame: .zero)
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = 50
        tableView.register(FJReportTableViewCell.self)

        return tableView
    }()
    lazy var submitButton               = c_make_ex(UIButton.self)
        .font(21.font_bold).color(.white).text("Submit").bgColor("#FF9133")
        .addTarget(self, selector: #selector(museAction_submit))
        .corner(25).end()
    lazy var closeButton                = c_make_ex(UIButton.self)
        .image("FJ_Common_close")
        .addTarget(self, selector: #selector(museAction_close))
        .corner(25).end()
    private var dataSource: [FJReportItemModel] =
    [FJReportItemModel(title: "Verbal Attack", select: false),
     FJReportItemModel(title: "Racism", select: false),
     FJReportItemModel(title: "Advertise", select: false),
     FJReportItemModel(title: "Pornography", select: false),
     FJReportItemModel(title: "Others", select: false)]
    
    var otherId: String = ""
    private var isSel = false
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        self.backgroundColor = .clear
        
        subviews {
            backView.subviews{
                titleLabel
                tableView
                submitButton
            }
            closeButton
        }
        
        backView.leading(15).trailing(15).height(440)
        backView.CenterY == self.CenterY - 50
        titleLabel.top(25).centerHorizontally()
        tableView.leading(0).trailing(0).height(250).top(65)
        submitButton.width(305).height(50).centerHorizontally()
        submitButton.Top == tableView.Bottom + 30
        closeButton.size(50).centerHorizontally()
        closeButton.Top == backView.Bottom + 25
        
        backView.isUserInteractionEnabled = true
//        var models = [FJProfileModel]()
//        dataSource.forEach { model in
//            var itemModel = model
//            itemModel.isSel = false
//            models.append(itemModel)
//        }
//        dataSource = models
//        self.tableView.reloadData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func museAction_close(_ sender: UIButton) {
//        muse.animation(dismiss: self)
        saAnimate.animation(dismiss: self)
    }
    
    @objc private func museAction_submit(_ sender: UIButton) {
//        sender.isUserInteractionEnabled = false
//        guard let content = textView.text, content.count > 0 else {
//            "Please tell us why you want to leave us. ".toast_error
//            return
//        }
        if isSel {
            
        }
    }
}

extension FJReportView: UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.cell(FJReportTableViewCell.self, indexPath: indexPath)
        cell.itemModel = dataSource[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        isSel = true
        dataSource[indexPath.row].select = !dataSource[indexPath.row].select
        tableView.reloadData()
        
    }
}

class FJReportTableViewCell: UITableViewCell {
    
    private let titleLabel          = c_make_ex(UILabel.self)
        .color(.black).font(17.font_bold).end()
    private let selButton           = c_make_ex(UIButton.self)
        .image("FJ_Message_Chat_sel",state: .selected)
        .image("FJ_Message_Chat_unSel").end()
    
    var itemModel: FJReportItemModel? {
        didSet{
            guard let md = itemModel else {
                return
            }
            titleLabel.text = md.title
            selButton.isSelected = md.select
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews {
            titleLabel
            selButton
        }
        titleLabel.leading(15).trailing(70).centerVertically()
        selButton.trailing(20).size(24).centerVertically()
        selButton.isUserInteractionEnabled = false
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
